//
//  SETank.cpp
//  Space Explorer
//
//  Created by Yann Blaudin de Thé on 13/05/13.
//  Copyright (c) 2013 TeaTime. All rights reserved.
//

#include "SETank.h"


SETank::SETank(double m_struct, double m_ox, double m_red, double mol_mass_ox, double mol_mass_red, double stoechio) : _m_struct(m_struct), _m_ox(m_ox), _m_red(m_red), _ox_mol_mass(mol_mass_ox), _red_mol_mass(mol_mass_red), _stoechio(stoechio) {
    
}

SETank::~SETank() {
    
}

double SETank::getMassRatio() const {
    return _m_struct/(_m_struct+_m_red+_m_ox);
}

double SETank::getTotalFlowRate() const {
    std::vector<SEEngine*>::iterator engine_iterator;
    
    double q = 0.;
    
    for (engine_iterator = _engines.begin();
         engine_iterator != _engines.end();
         engine_iterator++) {
        q += (*engine_iterator)->getFlowRate();
    }
    
    return q;
    
}

void SETank::update(double dt) {
    double dm_ox, dm_red;
    double q0;
    
    q0 = this->getTotalFlowRate()/(_red_mol_mass + _stoechio*_ox_mol_mass);
    
    dm_ox = _stoechio*q0*_ox_mol_mass*dt;
    dm_red = q0*_red_mol_mass*dt;
    
    if (_m_ox - dm_ox > 0.) {
        _m_ox -= dm_ox;
        _m_red -= dm_red;
    } else {
        _m_ox = 0.;
        _m_red = 0.;
    }
    
}

double SETank::getTotalMass() const {
    return _m_struct + _m_red + _m_ox;
}

